package com.linuxone.tools.client;

import java.util.List;
import java.util.Map;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.linuxone.news.NewsThread;
import com.linuxone.news.client.NewsService;
import com.linuxone.news.client.NewsServiceAsync;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Tools implements EntryPoint {

	private final ToolsServiceAsync toolsService = GWT.create(ToolsService.class);
	private final NewsServiceAsync newsService = GWT.create(NewsService.class);
	
//	final VerticalPanel newsContentVPanel = new VerticalPanel();
	final HTML newsContentHTML = new HTML();

	public void onModuleLoad() {
//		final HorizontalSplitPanel mainHPanel = new HorizontalSplitPanel();
//		mainHPanel.setLeftWidget(populateNewsThread());
//		mainHPanel.setRightWidget(newsContentVPanel);
//	    DecoratorPanel decPanel = new DecoratorPanel();
//	    decPanel.setWidget(mainHPanel);
	    
	    final FlexTable mainFlexTable = new FlexTable();
	    mainFlexTable.setWidget(0, 0, populateNewsThread());
	    mainFlexTable.getCellFormatter().setWordWrap(0, 0, false);
	    mainFlexTable.setWidget(0, 1, newsContentHTML);
	    mainFlexTable.getCellFormatter().setVerticalAlignment(0, 1, HasVerticalAlignment.ALIGN_TOP);
		
		final VerticalPanel mainVPanel = new VerticalPanel();
		mainVPanel.setSize("100%","100%");
	    final MenuBar menu = new MenuBar();
	    menu.setAutoOpen(true);
	    menu.setAnimationEnabled(true);
		populateMenu("GOOGLE", menu);
		populateMenu("LINUXONE", menu);
		mainVPanel.add(menu);
		//mainVPanel.add(populateNews());
		//mainVPanel.add(populateNewsThread());
		mainVPanel.add(mainFlexTable);
		
	    RootPanel.get("container").add(mainVPanel);
	}
	
	private VerticalPanel populateNewsThread(){
		final VerticalPanel VPanel = new VerticalPanel();
		VPanel.setWidth("300px");
		
		newsService.getNewsThreadinList(new AsyncCallback<List<NewsThread>>() {
			public void onFailure(Throwable caught) {
				System.out.println(caught.toString());
			}
			public void onSuccess(final List<NewsThread> newsList) {
				for(final NewsThread newsThread: newsList){
					HorizontalPanel HPanel = new HorizontalPanel();
					HPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
					Anchor anchor = new Anchor();
					anchor.setStyleName("newsTitle");
					anchor.setText(newsThread.getTitle());
					anchor.setName(newsThread.getId());
					anchor.setHref("#");
					HPanel.add(anchor);
					Label label = new Label();
					label.setStyleName("newsTitleDate");
					//label.setText(" - "+newsThread.getDateTime()+" @"+newsThread.getId());
					final DateTimeFormat formatter = DateTimeFormat.getFormat("dd/MM/yyyy HH:mm:ss");
					label.setText(" - "+formatter.format(newsThread.getDate()).toString());
					HPanel.add(label);
					VPanel.add(HPanel);
					
					anchor.addClickHandler(new ClickHandler(){
						public void onClick(ClickEvent event) {
//							newsContentVPanel.clear();
							newsContentHTML.setHTML("");
							Label labelTitle = new Label(newsThread.getTitle());
							labelTitle.setStyleName("newsContentTitle");
							newsContentHTML.setHTML(newsContentHTML.getHTML()+labelTitle.toString());
							Label labelDate = new Label(formatter.format(newsThread.getDate()).toString());
							labelDate.setStyleName("newsContentDate");
							newsContentHTML.setHTML(newsContentHTML.getHTML()+labelDate.toString());
							for(String content: newsThread.getContent()) {
								Label labelContent = new Label(content);
								labelContent.setStyleName("newsContent");
								newsContentHTML.setHTML(newsContentHTML.getHTML()+"<p>"+labelContent.toString()+"</p>");
							}
						}
					});
				}
			}
		});
		return VPanel;
	}
	
	private void populateItemList(MenuBar mBar, Map<String,String> ItemList) {
	    for (Map.Entry<String, String> entry : ItemList.entrySet()) {
			final Object name = entry.getKey();
			final Object value = entry.getValue();
			mBar.addItem(name.toString(), new Command() {
		    	public void execute() {
		        	Window.open(value.toString(), "", "");
		        }
		    });
		}
	}
	
	private void populateMenu(final String ListName, final MenuBar menu){
		toolsService.getItemList(ListName,
				new AsyncCallback<Map<String, String>>() {
					public void onFailure(Throwable caught) {
						// Show the RPC error message to the user
					}
					public void onSuccess(final Map<String,String> ItemList) {
					    MenuBar subMenu = new MenuBar(true);
					    subMenu.setAnimationEnabled(true);
					    populateItemList(subMenu,ItemList);
					    menu.addItem(ListName, subMenu);
					}
				}
		    );
	}

}
